package com.goods.link;

import org.eclipse.jetty.util.BlockingArrayQueue;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.util.LinkedList;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 固定获取窗口，并且有归还窗口的标记
 * @author Dan
 * Created by zhengjunbo on 2015/9/14.
 */
public class DriverUtil {

    private LinkedList<RemoteWebDriver> unUseDrivers = new LinkedList<>() ;
    private LinkedList<RemoteWebDriver> usedDrivers = new LinkedList<>();
    private final int count ;
    private BlockingQueue<String> queue = new BlockingArrayQueue<>();
    private final Lock lock = new ReentrantLock();
    private final Condition notFull = lock.newCondition();
    public DriverUtil(Integer count) {
        this.count = count;
    }

    public RemoteWebDriver getRemoteDriver(){
        try {
            lock.lockInterruptibly();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        while(!(checkDriverTotal()||checkUnUser())){  //检查总数不够的话等待 够的话直接给
            //等待

            try {
                System.out.println("等待前！");
                notFull.await();
                System.out.println("等待完成！");

            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }

        while (checkCanUseDriver()){ //如果整体到了最大数量，而且已经满了，也等待
            //等待
            try {
                notFull.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }


        RemoteWebDriver remoteWebDriver = getOneDriver(hasUnUseDriver());
        lock.unlock();
        return remoteWebDriver;
    }

    private boolean checkUnUser() {
        if(unUseDrivers.size()>0){
            return true;
        }
        return false;
    }

    private RemoteWebDriver getOneDriver(boolean b) {
        if(b){
            RemoteWebDriver peek = unUseDrivers.peek();
            usedDrivers.add(peek);
            return peek;
        }
        DemoDriver demoDriver = new DemoDriver();
        usedDrivers.add(demoDriver.getDriver());
        return demoDriver.getDriver();
    }

    private boolean hasUnUseDriver() {
        if(unUseDrivers.size()>0){
            return true;
        }
        return false;
    }

    private boolean checkCanUseDriver() {
        if(unUseDrivers.size()==0&&usedDrivers.size()==count){ //如果已使用的还未满
            return true;
        } else {
            return false;
        }

    }

    private boolean checkDriverTotal() {
        if((unUseDrivers.size()+usedDrivers.size())<count){
            return true;
        } else {
            return false;
        }
    }

    public void giveBack(final RemoteWebDriver driver){

        System.out.println("等待进入归还锁");
        lock.lock();
        System.out.println("已经进入归还锁");
        usedDrivers.remove(driver);
        unUseDrivers.add(driver);
        System.out.println("检查是否需要唤醒");
        if(checkDriverTotal()|| checkCanUseDriver()){
            System.out.println("正在唤醒");
            notFull.signalAll();
            System.out.println("已经唤醒");
        }
        System.out.println("已经归还");

        lock.unlock();
    }
}
